#include<bits/stdc++.h>
using namespace std;
namespace IO{
    char buff[1<<21],*p1=buff,*p2=buff;
    char getch(){
        return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;
    }
    template<typename T>
    void read(T &x){
        char ch=getch();int fl=1;x=0;
        while(ch>'9'||ch<'0'){if(ch=='-')fl=-1;ch=getch();}
        while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getch();}
        x*=fl;
    }
    template<typename T,typename ...Args>
    void read(T &x,Args& ...args){
        read(x);read(args...);
    }
    char obuf[1<<21],*p3=obuf;
    void putch(char ch){
        if(p3-obuf<(1<<21))*p3++=ch;
        else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;
    }
    char ch[100];
    template<typename T>
    void write(T x){
        if(!x)return putch('0');
        if(x<0)putch('-'),x*=-1;
        int top=0;
        while(x)ch[++top]=x%10+48,x/=10;
        while(top)putch(ch[top]),top--;
    }
    template<typename T,typename ...Args>
    void write(T x,Args ...args){
        write(x);write(args...);
    }
    void flush(){fwrite(obuf,p3-obuf,1,stdout);}
}
using namespace IO;
const int N=5e4+5,B=500;
inline int W(int x){return x/B;}
int n,m;
int fa[N],dep[N],dfn[N],id[N],tot;
int st[N][17];
vector<int>to[N];
void dfs(int x){
	dep[x]=dep[fa[x]]+1;
	id[dfn[x]+=++tot]=x;
	for(auto y:to[x])dfs(y);
}
inline int lca(int x,int y){
	if(dfn[x]>dfn[y])swap(x,y);
	int len=log2(dfn[y]-dfn[x]+1);
	int z=dep[st[dfn[x]][len]]<dep[st[dfn[y]-(1<<len)+1][len]]?st[dfn[x]][len]:st[dfn[y]-(1<<len)+1][len];
	if(z==x)return x;
	else return fa[z]; 
}
void Init(){
	dfs(1);
	for(int i=1;i<=n;i++)st[i][0]=id[i];
	for(int i=1;i<17;i++)
		for(int j=1;j+(1<<i)-1<=n;j++)
			st[j][i]=dep[st[j][i-1]]<dep[st[j+(1<<(i-1))][i-1]]?st[j][i-1]:st[j+(1<<(i-1))][i-1];
}
struct node{
	int l,r,id;
}q[N];
int a[N],ans[N];
set<pair<int,int>>s;
int vis[N],cnt;
inline void ad(int x){if(!vis[x])cnt++;vis[x]++;return;}
inline void de(int x){vis[x]--;if(!vis[x])cnt--;return;}
inline void add(int x){
	ad(x);
	s.insert(make_pair(dfn[x],x));
	auto tmp=s.find(make_pair(dfn[x],x));
	auto tmp2=tmp;tmp2++;
	if(tmp!=s.begin()){
		tmp--;
		ad(lca((*tmp).second,x));
		if(tmp2!=s.end())de(lca((*tmp).second,(*tmp2).second));
		tmp++;
	}
	tmp++;
	if(tmp!=s.end())ad(lca(x,(*tmp).second));
}
inline void del(int x){
	de(x);
	auto tmp=s.find(make_pair(dfn[x],x));
	auto tmp2=tmp;tmp2++;
	if(tmp!=s.begin()){
		tmp--;
		de(lca((*tmp).second,x));
		if(tmp2!=s.end())ad(lca((*tmp).second,(*tmp2).second));
		tmp++;	
	}
	tmp++;
	if(tmp!=s.end())de(lca(x,(*tmp).second));
	s.erase(make_pair(dfn[x],x));
}
inline void Add(int x){return add(a[x]);}
inline void Del(int x){return del(a[x]);} 
void sol(){
	sort(q+1,q+m+1,[](node A,node B){return W(A.l)!=W(B.l)?W(A.l)<W(B.l):A.r<B.r;});
	int l=1,r=1;Add(1);
	for(int i=1;i<=m;i++){
		while(l>q[i].l)Add(--l);
		while(r<q[i].r)Add(++r);
		while(l<q[i].l)Del(l++);
		while(r>q[i].r)Del(r--);
		ans[q[i].id]=cnt;
	}
}
int main(){
	freopen("escape.in","r",stdin);
	freopen("escape.out","w",stdout);
	read(n,m);
	for(int i=2;i<=n;i++)read(fa[i]);
	for(int i=2;i<=n;i++)to[fa[i]].push_back(i);
	for(int i=1;i<=n;i++)read(a[i]);
	for(int i=1;i<=m;i++)read(q[i].l,q[i].r),q[i].id=i;
	Init();
	sol();
	for(int i=1;i<=m;i++)write(ans[i]),putch('\n');
	flush();
	return 0;
}

